<?php
/*
* 2007-2011 PrestaShop 
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <contact@prestashop.com>
*  @copyright  2007-2011 PrestaShop SA
*  @version  Release: $Revision: 8005 $
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

if (!defined('_PS_VERSION_'))
	exit;

class GroupBuyingRegModule extends ObjectModel
{
	public $id_group_buying_company;
	public $id_group_buying_promotion;
	public $id_customer;
	public $coupon_codes;
	public $date_add;
	public $date_upd;

	protected $fieldsRequired = array('id_group_buying_company', 'id_group_buying_promotion');
	protected $fieldsSize = array('id_group_buying_company' => 2, 'id_customer' => 10, 'id_group_buying_promotion' => 3, 'coupon_codes' => 300);
	protected $fieldsValidate = array( 'id_group_buying_company' => 'isUnsignedId', 'id_customer' => 'isUnsignedId', 'id_group_buying_promotion' => 'isUnsignedId', 'coupon_codes' => 'isMessage');
	protected $table = 'group_buying_registration';
	protected $identifier = 'id_group_buying_registration';

	public function getFields()
	{
		parent::validateFields();
		$fields['id_group_buying_company'] = (int)$this->id_group_buying_company;
		$fields['id_customer'] = (int)$this->id_customer;
		$fields['id_group_buying_promotion'] = (int)$this->id_group_buying_promotion;
		$fields['coupon_codes'] = pSQL($this->coupon_codes);
		$fields['date_add'] = pSQL($this->date_add);
		$fields['date_upd'] = pSQL($this->date_upd);
		return $fields;
	}
	
	/**
	  * Return group buying companies
	  *
	  * @return array Companies
	  */
	public static function getGroupBuyingCompanies($id_lang = 0)
	{
		if (!$id_lang)
			$id_lang = _USER_ID_LANG_;
		
		if (!Validate::isUnsignedId($id_lang))
			die (Tools::displayError());
			
		$result = array();

		$companies = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
			SELECT `id_group_buying_company`, `company_name`, `url_link`
			FROM `'._DB_PREFIX_.'group_buying_company_lang`
			WHERE `active` = 1
			AND `id_lang` = '.(int)($id_lang));
		if (empty($companies) === true OR !sizeof($companies))
			return array();
		for ($i = 0; $i < sizeof($companies); ++$i)
		{
			$result[$i]['id_group_buying_company'] = $companies[$i]['id_group_buying_company'];
			$result[$i]['company_name'] = $companies[$i]['company_name'];
			$result[$i]['url_link'] = $companies[$i]['url_link'];
		}
		return ($result);
	}
	
	/**
	  * Return all group buying promotions
	  *
	  * @return array Promotions
	  */
	public static function getAllGroupBuyingPromotions($id_lang = 0)
	{
		if (!$id_lang)
			$id_lang = _USER_ID_LANG_;
		
		if (!Validate::isUnsignedId($id_lang))
			die (Tools::displayError());
			
		$result = array();

		$promotions = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
			SELECT `id_group_buying_promotion`, `id_group_buying_company`, `promotion_short_desc`, `promotion_desc`, `url_link`
			FROM `'._DB_PREFIX_.'group_buying_promotion_lang`
			WHERE `active` = 1
			AND `id_lang` = '.(int)($id_lang));
		if (empty($promotions) === true OR !sizeof($promotions))
			return array();
		for ($i = 0; $i < sizeof($promotions); ++$i)
		{
			$result[$i]['id_group_buying_promotion'] = $promotions[$i]['id_group_buying_promotion'];
			$result[$i]['id_group_buying_company'] = $promotions[$i]['id_group_buying_company'];
			$result[$i]['company_name'] = $promotions[$i]['company_name'];
			$result[$i]['promotion_short_desc'] = $promotions[$i]['promotion_short_desc'];
			$result[$i]['promotion_desc'] = $promotions[$i]['promotion_desc'];
			$result[$i]['url_link'] = $promotions[$i]['url_link'];
		}
		return ($result);
	}
	
	/**
	  * Return group buying registrations by customer id
	  *
	  * @return array group buying registrations
	  */
	public static function getRegistrationByCustomerId($id_customer, $id_lang = 0)
	{
	  if (!$id_lang)
			$id_lang = _USER_ID_LANG_;
		
		if (!Validate::isUnsignedId($id_lang))
			die (Tools::displayError());
	  
	  $registrations = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
	  SELECT r.`id_group_buying_registration`, r.`id_group_buying_company`, cl.`company_name`, r.`coupon_codes`, pl.`promotion_desc`, r.`date_add`, cl.`url_link` AS c_url_link, pl.`url_link` as p_url_link
	  FROM `'._DB_PREFIX_.'group_buying_registration` r
	  JOIN `'._DB_PREFIX_.'group_buying_company_lang` cl ON cl.`id_group_buying_company` = r.`id_group_buying_company` 
	  JOIN `'._DB_PREFIX_.'group_buying_promotion_lang` pl ON cl.`id_group_buying_company` = pl.`id_group_buying_company` AND cl.`id_lang` = pl.`id_lang`  
	  WHERE `id_customer` = '.(int)($id_customer).' 
	  AND cl.`id_lang` = '.(int)($id_lang).'
	  ORDER BY `date_add` DESC');
	  
	  $result = array();
	  
	  if (empty($registrations) === true OR !sizeof($registrations))
			return array();
		for ($i = 0; $i < sizeof($registrations); ++$i)
		{
			$result[$i]['id_group_buying_registration'] = $registrations[$i]['id_group_buying_registration'];
			$result[$i]['id_group_buying_company'] = $registrations[$i]['id_group_buying_company'];
			$result[$i]['company_name'] = $registrations[$i]['company_name'];
			$result[$i]['coupon_codes'] = $registrations[$i]['coupon_codes'];
			$result[$i]['promotion_desc'] = $registrations[$i]['promotion_desc'];
			$result[$i]['c_url_link'] = $registrations[$i]['c_url_link'];
			$result[$i]['p_url_link'] = $registrations[$i]['p_url_link'];
			$result[$i]['reg_date'] = $registrations[$i]['date_add'];
		}
		return ($result);
	}
	
	/**
	  * Return group buying company registered by registration id
	  *
	  * @return id_group_buying_company
	  */
	public static function getCompanyIdByRegId($id_group_buying_registration)
	{
		if (!Validate::isUnsignedId($id_group_buying_registration))
			die (Tools::displayError());
	  
	  $result = Db::getInstance()->getRow('
	  SELECT `id_group_buying_company`
	  FROM `'._DB_PREFIX_.'group_buying_registration`
	  WHERE `id_group_buying_registration` = '.(int)($id_group_buying_registration));
	  
	  if (isset($result['id_group_buying_company']))
			return ($result['id_group_buying_company']);
		return false;
	}
	
		/**
	  * Return group buying promotion registered by registration id
	  *
	  * @return id_group_buying_promotion
	  */
	public static function getPromotionIdByRegId($id_group_buying_registration)
	{
		if (!Validate::isUnsignedId($id_group_buying_registration))
			die (Tools::displayError());
	  
	  $result = Db::getInstance()->getRow('
	  SELECT `id_group_buying_promotion`
	  FROM `'._DB_PREFIX_.'group_buying_registration`
	  WHERE `id_group_buying_registration` = '.(int)($id_group_buying_registration));
	  
	  if (isset($result['id_group_buying_promotion']))
			return ($result['id_group_buying_promotion']);
		return false;
	}
	
	/**
	  * Return group buying coupon codes by registration id
	  *
	  * @return coupon codes
	  */
	public static function getCouponCodeByRegId($id_group_buying_registration)
	{
		if (!Validate::isUnsignedId($id_group_buying_registration))
			die (Tools::displayError());
	  
	  $result = Db::getInstance()->getRow('
	  SELECT `coupon_codes`
	  FROM `'._DB_PREFIX_.'group_buying_registration`
	  WHERE `id_group_buying_registration` = '.(int)($id_group_buying_registration));
	  
	  if (isset($result['coupon_codes']))
			return ($result['coupon_codes']);
		return false;
	}
	
	/**
	  * Return true or false when updating the group_buying_registration by id_group_buying_registration
	  *
	  * @return boolean
	  */
	public static function setRegistrationByRegId($id_group_buying_registration, $id_group_buying_company, $coupon_codes)
	{
		$result = (Db::getInstance()->Execute('
			UPDATE `'._DB_PREFIX_.'group_buying_registration` 
			SET id_group_buying_company = '.(int)($id_group_buying_company).', coupon_codes = \''.pSQL($coupon_codes).'\' 
			WHERE id_group_buying_registration = '.(int)($id_group_buying_registration)));
		return ($result);
	}
	
	/**
	  * Return true or false when deleting the group_buying_registration by id_group_buying_registration
	  *
	  * @return boolean
	  */
	public static function deleteRegistrationByRegId($id_group_buying_registration)
	{
		$result = (Db::getInstance()->Execute('
			DELETE FROM `'._DB_PREFIX_.'group_buying_registration`
			WHERE id_group_buying_registration = '.(int)($id_group_buying_registration)));
		return ($result);
	}
	
	/**
	  * Return true or false whether the selected group buying company has the offer selected
	  *
	  * @return boolean
	  */
	public static function isValidPromotion($id_group_buying_company, $id_group_buying_promotion)
	{
		$result = Db::getInstance()->getRow('
		SELECT `id_group_buying_promotion`
		FROM `'._DB_PREFIX_.'group_buying_promotion_lang`
		WHERE `id_group_buying_promotion` = '.$id_group_buying_promotion.'
		AND `id_group_buying_company` = '.$id_group_buying_company);

		return isset($result['id_group_buying_promotion']);
	}
	/**
	  * Return true or false whether there is an existing registration for the same customer and group buying company
	  *
	  * @return boolean
	  */
	public static function registrationExists($id_customer, $id_group_buying_company)
	{
		$result = Db::getInstance()->getRow('
		SELECT `id_group_buying_registration`
		FROM `'._DB_PREFIX_.'group_buying_registration`
		WHERE `id_customer` = '.$id_customer.'
		AND `id_group_buying_company` = '.$id_group_buying_company);

		return isset($result['id_group_buying_registration']);
	}
}